/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.rsc.feeds.client.gui.richTextArea;

import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 
 * @author arbuckle
 */
public class RichTextAreaWithToolbar extends Composite implements HasValue<String> {
    public static String decodeUrlDelimiters(String s) {
	if (s == null) {
	    return null;
	}

	s = s.replaceAll("%2F", "");
	s = s.replaceAll("%2F", "");
	s = s.replaceAll("%3A", "");
	s = s.replaceAll("%3F", "");
	s = s.replaceAll("%26", "");
	s = s.replaceAll("%3D", "");
	s = s.replaceAll("%2B", "");
	s = s.replaceAll("%24", "");
	s = s.replaceAll("%2C", "");
	s = s.replaceAll("%23", "");

	return s;
    }

    private VerticalPanel main = new VerticalPanel();

    private RichTextArea rta = new RichTextArea();

    public RichTextAreaWithToolbar() {
	this.rta = new RichTextArea();
	//this.rta.setHTML(data.getComment());
	this.rta.setWidth("100%");

	RichTextToolbar toolBar = new RichTextToolbar(this.rta);

	this.main.setWidth("100%");
	this.rta.setWidth("100%");
	this.main.add(toolBar);
	this.main.add(this.rta);
	initWidget(this.main);
    }

    @Override
    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> vch) {
	throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getValue() {
	return this.rta.getHTML();
    }

    @Override
    public void setValue(String t) {
	String html = t != null ? URL.decode(t) : null;
	if (html != null) {
	    html = RichTextAreaWithToolbar.decodeUrlDelimiters(html);
	}
	this.rta.setHTML(html);
    }

    @Override
    public void setValue(String t, boolean bln) {
	this.rta.setHTML(t);
    }
}
